package com.atguigu.gulimall.product.service.impl;

import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;
import com.atguigu.gulimall.product.dao.ProductAttrValueDao;
import com.atguigu.gulimall.product.entity.ProductAttrValueEntity;
import com.atguigu.gulimall.product.service.ProductAttrValueService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.stream.Collectors;


@Service("productAttrValueService")
public class ProductAttrValueServiceImpl extends ServiceImpl<ProductAttrValueDao, ProductAttrValueEntity> implements ProductAttrValueService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ProductAttrValueEntity> page = this.page(
                new Query<ProductAttrValueEntity>().getPage(params),
                new QueryWrapper<ProductAttrValueEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveProductAttr(List<ProductAttrValueEntity> collect) {
        this.saveBatch(collect);
    }

    @Override
    public List<ProductAttrValueEntity> baseAttrListforspu(Long spuId) {
        List<ProductAttrValueEntity> valueEntities = this.list(new LambdaQueryWrapper<ProductAttrValueEntity>().eq(ProductAttrValueEntity::getSpuId, spuId));
        return valueEntities;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSpuAttr(Long spuId, List<ProductAttrValueEntity> entities) {
        //1.删除spuid之前的所有属性
        this.baseMapper.delete(new LambdaQueryWrapper<ProductAttrValueEntity>().eq(ProductAttrValueEntity::getSpuId, spuId));
        List<ProductAttrValueEntity> collect = entities.stream().map(m -> {
            m.setSpuId(spuId);
            return m;
        }).collect(Collectors.toList());
        this.saveBatch(collect);

    }


    /**
     * 拼手气红包算法  ===fuzm
     *
     * @param money
     * @param num
     * @return
     * @author fuzm
     * @date 2021/8/6
     */
    public static ArrayList<Integer> send(int money, int num) {
        //TODO 拼手气红包算法  ===fuzm
        Random r = new Random();
        ArrayList<Integer> ss = new ArrayList<>();
        Integer middle = Math.round(money / num);
        Integer redMoney = 0;
        Integer nextMoney = money;
        Integer sum = 0;
        for (int i = num; i > 0; i--) {
            if (i == 1) {
                ss.add(nextMoney);
            } else {
                while (true) {
                    int str = r.nextInt(nextMoney);
                    redMoney = str;
                    if (redMoney > 0 && redMoney < middle) {
                        break;
                    }
                }
                nextMoney = nextMoney - redMoney;
                sum = sum + redMoney;
                ss.add(redMoney);
                middle = nextMoney / (i - 1);
            }
        }
        return ss;
    }

}